'use strict';

const express = require('express');
const gputility = require('../../common/gputility');
const logger = require('../../common/logutility').logger('regExpCmp');
let router = express.Router();

router.get('/', function(req, res) {
    gputility.excuteGP(getGP(req, res));
});

router.post('/', function(req, res) {
    gputility.excuteGP(postGP(req, res));
});

function* getGP(req, res) {
    try {
        res.render('./cou/regExpCmp');
    } catch (e) {
        logger.error(e);
        res.redirect('/error');
    }
}

function* postGP(req, res) {
    try {
        if (req.body.cmdType === 'L') {
            req.session.expReg = null;
            res.redirect('/regExpInp');
        } else if (req.body.cmdType === 'R') {
            req.session.expDlv = Object.assign(req.session.expReg);
            req.session.expReg = null;
            res.redirect('/dlvExpCho');
        }
    } catch (e) {
        logger.error(e);
        res.redirect('/error');
    }
}

module.exports = router;